<template>
  <div class="wrapper-list">
    <!-- 数据列表区 -->
    <div class="wrapper-table">
      <el-table
        v-loading="flagLoading"
        :data="tblDataList"
        :tree-props="{ children: 'children' }"
        :header-cell-style="{ 'text-align': 'center' }"
        row-key="id"
        default-expand-all
        fit
        size="small"
        highlight-current-row
        style="width: 100%"
        height="100%"
      >
        <el-table-column label="菜单名称" width="220" prop="title" />
        <el-table-column label="Url" width="220" prop="clientUrl" />
        <el-table-column label="路由名称" width="220" prop="routeName" />
        <el-table-column label="组件路径" prop="componentPath" />
        <el-table-column label="转向地址" width="220" prop="redirect" />
        <el-table-column align="center" label="图标" width="50">
          <template #default="scope">
            <component v-if="scope.row.icon !== ''" :is="scope.row.icon" style="width: 20px; height: 20px" />
          </template>
        </el-table-column>
        <el-table-column align="center" label="显示" width="50" prop="flagHidden">
          <template #default="scope">
            <el-icon v-if="scope.row.flagHidden" :size="16" color="#185210">
              <CircleCheckFilled />
            </el-icon>
            <el-icon v-else :size="16" color="#ff0000"><RemoveFilled /></el-icon>
          </template>
        </el-table-column>
        <el-table-column align="center" label="子菜单" width="60" prop="flagShowLeft">
          <template #default="scope">
            <el-icon v-if="scope.row.flagShowLeft" :size="16" color="#185210">
              <CircleCheckFilled />
            </el-icon>
            <el-icon v-else :size="16" color="#ff0000"><RemoveFilled /></el-icon>
          </template>
        </el-table-column>
        <el-table-column align="center" label="排序" width="50" prop="weight" />
        <el-table-column align="center" label="操作" width="90">
          <template #default="scope">
            <div class="wrapper-op">
              <a class="ibtn ibtn-edit" title="修改" @click="onEdit(scope.row)">
                <el-icon><Edit /></el-icon>
              </a>
              <a class="ibtn ibtn-del" title="删除" @click="onDelete(scope.row.id)">
                <el-icon><Delete /></el-icon>
              </a>
            </div>
          </template>
        </el-table-column>
      </el-table>
    </div>
  </div>
</template>

<script>
import { toastSuccess } from '@/utils/toast'
import { setSStorageObj } from '@/utils/web-storage'
import { AryToTree } from '@/utils/array'

import { ElMessageBox } from 'element-plus'

import router from '@/router'
import store from '@/store'
import { defineComponent, ref, onMounted } from 'vue'
export default defineComponent({
  name: 'RouteListAllView',
  setup() {
    const tblDataList = ref([])
    const flagLoading = ref(true)

    const listAll = () => {
      flagLoading.value = true
      store
        .dispatch('sys_front_route/listAll')
        .then((response) => {
          flagLoading.value = false
          tblDataList.value = AryToTree(response, 1)
        })
        .catch((error) => {
          flagLoading.value = false
          console.log('读取前台路由列表数据出现错误：', error)
        })
    }
    const onEdit = (row) => {
      if (row.id === 1) return
      setSStorageObj('ParamData', row)
      router.push({ path: 'edit' })
    }
    const onDelete = (id) => {
      if (id === 1) return
      ElMessageBox.confirm('确定要删除该路由吗？', '删除警告', {
        confirmButtonText: '确定删除',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(() => {
          store
            .dispatch('sys_front_route/delById', id)
            .then(() => {
              toastSuccess('删除成功！')
              listAll()
            })
            .catch((error) => {
              console.log('删除路由出现错误：', error)
            })
        })
        .catch(() => {})
    }

    onMounted(() => {
      listAll()
    })

    return {
      tblDataList,
      flagLoading,
      listAll,
      onEdit,
      onDelete,
    }
  },
})
</script>

<style lang="scss" scoped>
p {
  color: #185210;
}
</style>
